package com.leetcode.algorithm.y22.m08.w4;

/**
 * 1455. 检查单词是否为句中其他单词的前缀
 * 
 * https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/
 * 
 * @author jie.deng
 *
 */
class Question1455Solution01 {

	public int isPrefixOfWord(String sentence, String searchWord) {
		int cnt = 0;
		int idx = 0;
		int len = sentence.length();
		while (idx < len) {
			while (idx < len && sentence.charAt(idx) == ' ') {
				idx++;
			}
			if (idx == len) {
				break;
			}
			cnt++;
			int left = idx;
			while (idx < len && sentence.charAt(idx) != ' ') {
				idx++;
			}
			if (sentence.substring(left, idx).startsWith(searchWord)) {
				return cnt;
			}
		}
		return -1;
	}

}